import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 1500;
    height: 1000;

    Rectangle {
        anchors.fill:parent;

        Rectangle {
            id: imageCont
            anchors.bottom: anchors.top;
            anchors.bottomMargin: fileButton.anchors.bottomMargin;

            width: parent.width;
            height: parent.height - imageCont.anchors.bottomMargin*2 - fileButton.height;

            Image {
                id: imageViewer;
                asynchronous: true;
                anchors.fill: parent;
                fillMode: Image.PreserveAspectFit;

                onStatusChanged: {
                    if(imageViewer.status == Image.Loading){
                        bi.visible = true;
                        bi.running = true;
                    }else if(imageViewer.status == Image.Ready){
                        bi.running = false;
                        bi.visible = false;
                    }
                }
            }

            BusyIndicator {
                id: bi;
                anchors.centerIn: parent;
                running: false;
                z: Image.z + 1;
            }
        }

        Button {
            id: fileButton;

            anchors.left: parent.left;
            anchors.leftMargin: 10;
            anchors.bottom: parent.bottom;
            anchors.bottomMargin: 10;

            width: 90;
            height: 40;

            style: ButtonStyle {
                background: Rectangle{
                    anchors.fill: parent;
                    color: "deepskyblue";
                    border.width: control.pressed ? 2 : 1;
                    border.color: control.pressed ? "white" : this.color;

                    Text {
                        anchors.centerIn: parent;
                        text: "File";

                        font {
                            family: "微软雅黑";
                            weight: Font.Light;
                            pixelSize:24;
                        }

                        color: "white";
                    }
                }
            }

            onClicked: {
                imageSelect.open();
            }
        }

        Text {
            id: textFileUrl;

            anchors.left: fileButton.right;
            anchors.leftMargin: 10;
            anchors.verticalCenter: fileButton.verticalCenter;

            color: "deepskyblue";

            font{
                family: "微软雅黑";
                weight: Font.Light;
                pixelSize: 20;
            }
        }

        FileDialog {
            id: imageSelect;

            nameFilters: ["Image Files (*.jpg *png *gif)"];

            onAccepted: {
                imageViewer.source = imageSelect.fileUrl;
                var imageString = new String(imageSelect.fileUrl);
                textFileUrl.text = imageString.slice(8);
            }
        }
    }
}
